<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

// 接口（后台）
Route::match(
    ['post', 'get',],
    '/test/{controller?}/{action?}',
    function($controller = 'frontend', $action = 'index') {
        try{
            $request = request();
            $post    = $request->all();

            // 控制器   中划线 => 驼峰式:大小写
            $controller = ucfirst($controller);

            $file   = 'App\Http\Controllers\Test\\'.$controller.'Controller';
            $file   = str_replace(' ', '', ucwords(str_replace('-', ' ', $file)));
            $action = str_replace(' ', '', ucwords(str_replace('-', ' ', $action)));
            $action = lcfirst($action);

            // var_dump($file);die;
            // 执行请求
            $file   = App::make($file);
            $result = $file->$action($post);

            return $result;
        } catch(Exception $e){
            return ['code' => $e->getCode(), 'msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()];
        }
    }
)->where(['controller' => '[0-9a-zA-Z-]+', 'action' => '[0-9a-zA-Z-]+'])->name('backend'); // 统一命令

